import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import {
  GetindexbtnApi,
  GetindexartApi,
  GetTopicApi,
  GetProductApi,
  GetDetailApi,
} from "../utils/api";
export default new Vuex.Store({
  state: {
    //首页的按钮列表
    indexlist: [],
    //首页文章列表
    articlelist: [],
    //话题列表
    Topiclist: [],
    Productlist: [],
    DetailData: [],
  },
  mutations: {
    //首页的按钮列表
    Getindexlist(state, payload) {
      state.indexlist = payload;
    },
    Getindexart(state, payload) {
      state.articlelist = payload;
    },
    //修改话题列表
    GetTopiclist(state, payload) {
      state.Topiclist = payload;
    },
    //获取商品的列表
    GetProductlist(state, payload) {
      state.Productlist = payload;
    },
    //获取详情数据
    GetDetail(state, payload) {
      state.DetailData = payload;
    },
  },
  actions: {
    //首页的按钮列表
    async Getindexlistasync({ commit }) {
      const res = await GetindexbtnApi();
      if (res.status === 0) {
        commit("Getindexlist", res.result);
      }
    },
    //首页的文章列表
    async Getindexartasync({ commit }) {
      const res = await GetindexartApi();
      if (res.status === 0) {
        commit("Getindexart", res.result);
      }
    },
    //获取话题列表
    async GetTopiclistasync({ commit }) {
      const res = await GetTopicApi();
      if (res.status === 0) {
        commit("GetTopiclist", res.result);
      }
    },
    //获取商品的列表
    async GetProductlistasync({ commit }) {
      const res = await GetProductApi();
      if (res.status === 0) {
        commit("GetProductlist", res.result);
      }
    },
    //获取详情页数据
    async GetDetailasync({ commit }) {
      const res = await GetDetailApi();
      if (res.status === 0) {
        commit("GetDetail", res.result);
      }
    },
  },
  getters: {
    //获取文章的推荐
    recommend(state) {
      return state.articlelist.slice(0, 2);
    },
    //banner后面的文章
    unrecommend(state) {
      return state.articlelist.slice(2);
    },
    //产品页面 每个数据只要前三条
    //文章
    proarticle(state) {
      return state.articlelist.slice(0, 3);
    },
    //话题前三
    Topiclistbefore(state) {
      return state.Topiclist.slice(0, 3);
    },
    //获取商品的列表前三
    Productlistbefore(state) {
      return state.Productlist.slice(0, 3);
    }
  },
  modules: {},
});
